package com.gab.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gab.utils.Result;


@RestController
@RequestMapping("/login")
public class Login {
	private static Properties properties = null;
	private static FileOutputStream out = null;
	static {
		try {
			properties = new Properties();
			properties.load(Login.class.getClassLoader().getResourceAsStream("login.properties"));
			out=new FileOutputStream(Login.class.getClassLoader().getResource("login.properties").getPath());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/login")
	public boolean login(String password) throws IOException {
		if (password != null && password.equals(properties.getProperty("password"))) {
			return true;
		} else {
			return false;
		}
	}

	@RequestMapping("/updatePassword")
	public Result updatePassword(String oldPassword, String newPassword) throws IOException {
		if (oldPassword != null && oldPassword.equals(properties.getProperty("password"))) {
			if (newPassword.length() >= 6 && newPassword.length() <= 16) {
				properties.put("password", newPassword);
				properties.store(out,null);
				return new Result(true, "修改成功!");
			} else {
				return new Result(false, "密码长度有误 , 最少 6 位 最长 16 位!");
			}
		} else {
			return new Result(false, "原密码错误!");
		}
	}

/*	@Test
	public void test() throws IOException {
		Properties properties = new Properties();
		properties.load(this.getClass().getClassLoader().getResourceAsStream("config/login.properties"));
		//读取
		System.out.println("_ 读取 _"+properties.getProperty("password"));
		//更换
		properties.put("password","123456");
		FileOutputStream out=new FileOutputStream(Login.class.getClassLoader().getResource("config/login.properties").getPath());
		properties.store(out, "");
	}*/
}
